<?php
namespace app\admin\model;
use think\Model;
use think\Db;

class DelayedSendingSmsM extends Model
{
    protected $table = 'bus_selfdom_info';

    public function getAllData($request)
    {
        $data = $this::alias('bt')
            ->field(
                'bt.id
                ,bt.batch_num 
                , bt.channel_id 
                , bt.pull_total 
                , bt.username 
                , bt.send_time 
                , bt.create_time 
                , bt.update_time 
                , bt.update_user 
                , bt.message 
                , bt.sms_type 
                , bt.status as status2 
                , bc.company_name
                , fs.status
                , fs.total
                , fs.fail
                , fs.success
                , fs.create_time
                , fs.done_time
                ')
            ->join('bus_channel bc', 'bc.channel_id=bt.channel_id')
            ->join('file_status fs', 'fs.order_id=bt.batch_num','left')
            ->where('bt.create_time', 'between time', [$request['st'], $request['et']])
            ->where('bt.create_time', '>=', '2019-12-06')
            // ->where('bt.order_type','in', $request['order_type'] )
            ->where('bt.sms_type','in', $request['sms_type'] );

        //渠道通道用户只能查到自己的订单
        switch (session('user_type')) {

            case 'admin':

                if (!empty($request['cha'])) {

                    $data = $data->where('bt.channel_id', $request['cha']);
                }
                break;

            case 'channel':

                $data = $data->where('bt.channel_id', session('relate_id'));
                break;

            default:

                return [];
                break;
        }

        if ($request['status'] !== '') {

            $data = $data->where('bt.status', $request['status']);
        }
        if ($request['cha'] !== '') {

            $data = $data->where('bt.channel_id', $request['cha']);
        }

        $dataarr = $data->order('bt.create_time desc')
                ->paginate($request['limit'], false, ['query'=>$request])
                ->each(function($item, $key){

                    if($item->pull_total == 0){
                        $item->pull_total = '';
                    }
                    
                    switch ($item->status) {
                        case '0':
                            $item->status_ = '<span class="layui-badge layui-bg-orange event-stop">未处理</span>';
                            break;
                        case '1':
                            $item->status_ = '<span class="layui-badge layui-bg-green">已完成</span>';
                            break;
                        // case '2':
                        //     $item->status_ = '<span class="layui-badge">已完成</span>';
                        //     break;

                        default:

                            if(cache("curlsendstatus_".$item->id)){
                                $a = cache("curlsendstatus_".$item->id);
                            }else{
                                $a = $this->curlsendstatus($item->id);
                                cache("curlsendstatus_".$item->id , $a , 3);
                            }

                            if($a['code'] == 200){
                                $item->status_ = '处理中';

                                $item->total = $a['total'];
                                $item->success = $a['success'];
                                $item->fail = $a['fail'];
                            }else{
                                $item->status_ = '处理中';
                            }
                            break;
                    }

                    if($item->status2 == 2){
                        $item->status_ = '<span class="layui-badge">已取消</span>';
                    }elseif($item->status2 == 0 && $item->status == '' && $item->batch_num == ''){
                        $item->status_ = '等待发送';
                    }

                    switch ($item->sms_type) {
                        case '1':
                            $item->sms_type = '普通短信';
                            break;
                        case '2':
                            $item->sms_type = '视频短信';
                            break;

                        default:
                            $item->sms_type = '';
                            break;
                    }

                    $item->phone_sum = '查询中';
                    $item->success_sum = '查询中';
                    $item->failure_sum = '查询中';
                    $item->unknown_sum = '查询中';
                    // $item->batch_num = $item->id;


                    // if (!empty($item->phones)) {

                    //     $item->phones_ = '<a href="javascript:;" onclick="findPhone('.$item->id.')" style="color:#3498db"><u>查看</u></a>';
                    // } else {

                    //     $item->phones_ = '';
                    // }

                });
        //返回数据集
        return $dataarr;

    }

    public function getInfo($where)
    {
        return $this->where($where)->find();
    }

    public function getWhereInfo($where)
    {
        return $this->where($where)->select();
    }

    public function updateInfo($data, $where)
    {
        if ($this->save($data, $where)) {

            return ['code' => 0, 'msg' => '编辑成功'];
        } else {

            return ['code' => 1, 'msg' => '编辑失败'];
        }
    }

    /**
     * 查询状态
     * @return [type] [description]
     */
    public function curlsendstatus($id)
    {
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);

        $post_data = array(
                'id' => $id
            );

        // trace("上传数据" . json_encode($post_data,JSON_UNESCAPED_UNICODE),"send");

        curl_setopt($curl, CURLOPT_URL, 'http://119.91.61.33:8080/SMS/status');

        curl_setopt($curl, CURLOPT_POST, 1 );

        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        curl_setopt($curl, CURLOPT_USERAGENT,"TEST");

        $result = curl_exec($curl);

        // trace("上传结果" . $result,"send");

        // $error = curl_error($curl);

        $result = json_decode($result,true);

        //关闭URL请求
        curl_close($curl);

        return $result;
    }

}